/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-01-26     Administrator       the first version
 *
 */
#ifndef APPLICATIONS_MQTT_MQTT_H_
#define APPLICATIONS_MQTT_MQTT_H_

#include "paho_mqtt.h"
#include "cJSON.h"

#define DEFAULT_MQTT_URI            "tcp://192.168.1.150:1883" // 配置测试服务器地址
#define DEFAULT_MQTT_CLIENTID       "ctl00001"             // 设置client_id
#define DEFAULT_MQTT_USERNAME       "admin"
#define DEFAULT_MQTT_PASSWORD       "public"
#define DEFAULT_MQTT_SUBTOPIC       "fsc/control/ctl00001/rx" // 设置订阅主题
#define DEFAULT_MQTT_PUBTOPIC       "fsc/control/ctl00001/tx" // 设置推送主题
#define DEFAULT_MQTT_WILLMSG        "Goodbye!"           // 设置遗言消息

typedef struct MQTTConfig
{
    char MQTT_URI[64];
    char MQTT_CLIENTID[10];
    char MQTT_USERNAME[10];
    char MQTT_PASSWORD[20];
    char MQTT_SUBTOPIC[30];
    char MQTT_PUBTOPIC[30];
    char MQTT_WILLMSG[30]; // mqtt遗言
} MQTTConfig;

int mqtt_start();
int mqtt_publish(const char *msg_str);
int mqtt_subscribe(const char *topic);
int mqtt_unsubscribe(const char *topic);

void mqtt_send_msg(int code, char *msg);
int mqtt_is_connected();

void mqtt_set_conf(struct MQTTConfig cfg);
struct MQTTConfig mqtt_get_conf();

// 回调函数
typedef void (*MQTT_RECV_CALLBACK)(cJSON *code, cJSON *msg);
MQTT_RECV_CALLBACK mqtt_recv_callback;

#endif /* APPLICATIONS_MQTT_MQTT_H_ */
